﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    class CLSMAEUSUP:ObservableObject
    {
        MDOgeneral mod = new MDOgeneral();

        //Detemrina si el usuario y la clave ingresado son correctos
        public Boolean ClaveValida() 
        {
            try
            {
                string query = "select count(*) from maeusup where codusu_usu='" + CODUSU_USU + "' and pasusu_usu='" + PASUSU_USU + "' and ACTUSU_USU='A'";
                DataTable val = mod.consulta(query);
                if (val.Rows.Count > 0)
                {
                    if (Convert.ToInt32(val.Rows[0][0]) == 1)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex) { throw ex; }
        }

        public string EmpresaImprime()
        {
            try
            {
                string strRetorno = "";
                string query = "select razsoc_cfg from cfgempp ";
                DataTable val = mod.consulta(query);
                if (val.Rows.Count > 0)
                {
                    foreach (DataRow r in val.Rows)
                    {
                        strRetorno = r["RAZSOC_CFG"].ToString();
                    }
                }
                return strRetorno;
            }
            catch (Exception ex) { throw ex; }
        }


        public List<CLSMAEUSUP> listarUsuario() 
        {
            try 
            {
                string query = "select CODUSU_USU,NOMUSU_USU from maeusup";
                DataTable val = mod.consulta(query);

                List<CLSMAEUSUP> lista = new List<CLSMAEUSUP>();
                foreach (DataRow r in val.Rows) 
                {
                    lista.Add(new CLSMAEUSUP {
                        CODUSU_USU = r["CODUSU_USU"].ToString(),
                        NOMUSU_USU = r["NOMUSU_USU"].ToString()
                    });
                }

                return lista;
            }
            catch (Exception ex){ throw ex;};
        }

        public List<CLSMAEUSUP> listarUsuarios()
        {
            try
            {
                string query = "select CODUSU_USU,NOMUSU_USU, PASUSU_USU, ACTUSU_USU, USAINDGES_USU from maeusup";
                DataTable val = mod.consulta(query);

                List<CLSMAEUSUP> lista = new List<CLSMAEUSUP>();
                foreach (DataRow r in val.Rows)
                {
                    lista.Add(new CLSMAEUSUP
                    {
                        CODUSU_USU = r["CODUSU_USU"].ToString(),
                        NOMUSU_USU = r["NOMUSU_USU"].ToString(),
                        PASUSU_USU = r["PASUSU_USU"].ToString(),
                        ACTUSU_USU = r["ACTUSU_USU"].ToString(),
                        USAINDGES_USU = r["USAINDGES_USU"].ToString()
                    });
                }

                return lista;
            }
            catch (Exception ex) { throw ex; };
        }

        public CLSMAEUSUP consultar() 
        {
            try 
            {
                string query = "select nomusu_usu,codusu_usu, pasusu_usu,actusu_usu, usaindges_usu from maeusup where upper(codusu_usu)=upper('" + CODUSU_USU + "')";
                DataTable val=mod.consulta(query);

                NOMUSU_USU = "";
                PASUSU_USU = "";
                ACTUSU_USU = "I";
                USAINDGES_USU = "N";
                foreach (DataRow dr in val.Rows) {
                    CODUSU_USU = dr["codusu_usu"].ToString();
                    NOMUSU_USU = dr["nomusu_usu"].ToString();
                    PASUSU_USU = dr["pasusu_usu"].ToString();
                    ACTUSU_USU = dr["actusu_usu"].ToString();
                    USAINDGES_USU = dr["usaindges_usu"].ToString();
                }
                return this;
            }
            catch (Exception ex) { throw ex; }
        }


        public DataTable buscar()
        {
            try
            {
                string query = "select CODUSU_USU as Codigo, NOMUSU_USU as Nombre from maeusup where upper(NOMUSU_USU) like upper('%" + NOMUSU_USU + "%') ";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }


        private string _CODUSU_USU;
        public string CODUSU_USU 
        {
            get { return _CODUSU_USU; }
            set { _CODUSU_USU = value; RaisePropertyChangedEvent("CODUSU_USU"); }
        }

        private string _NOMUSU_USU;
        public string NOMUSU_USU 
        {
            get { return _NOMUSU_USU; }
            set { _NOMUSU_USU = value; RaisePropertyChangedEvent("NOMUSU_USU"); }
        }

        private string _PASUSU_USU;
        public string PASUSU_USU 
        {
            get { return _PASUSU_USU; }
            set { _PASUSU_USU = value; RaisePropertyChangedEvent("PASUSU_USU"); }
        }

        private string _ACTUSU_USU;
        public string ACTUSU_USU 
        {
            get { return _ACTUSU_USU;  }
            set { _ACTUSU_USU = value; RaisePropertyChangedEvent("PASUSU_USU"); }
        }

        private string _USAINDGES_USU;
        public string USAINDGES_USU
        {
            get { return _USAINDGES_USU; }
            set { _USAINDGES_USU = value; RaisePropertyChangedEvent("PASUSU_USU"); }
        }

    }
}
